/*
 * LegoBot v1.0-a
 * Copyright (c) 2009 MindstormsKid <mindstormskd@gmail.com>
 * Licensed under the MIT license <http://www.opensource.org/licenses/mit-license.php>
 *
 * LegoBot IRC module <http://legocode.googlecode.com/svn/trunk/javascript/legobot/irc.js>
 */
/*
TODO:
CTCP support
add IRC.trigger, like LegoBot.trigger but with filters, in the form of an object like an IRCE, with the same fields. regexps for testing or other objects for comparing (==)
and IRC.next (or something) which is like $.fn.one()
redo the regexps.. someday..
redo a few more things
*/
var IRC = { cmds: {}, modes: {} };

IRC.connect = function(s){
	IRC.nick = s.nick;
	LegoBot.connect(s.host, s.port, handle, function(){
		if (s.pass) { IRC.send("PASS " + s.pass); }
		IRC.send("NICK " + s.nick);
		IRC.send("USER " + s.user + " " + s.host + " " + s.host + " :" + s.realname);
		(s.callback || function(){})();
	});
	
	function handle(msg){
		
	}
};


/********* basic actions *********/
IRC.send = LegoBot.send;

IRC.say = function(msg, chan){ IRC.send("PRIVMSG " + chan + " :" + msg); };

IRC.notice = function(msg, chan){ IRC.send("NOTICE " + chan + " :" + msg); };

IRC.action = function(msg, chan){ IRC.say("\u0001ACTION " + msg + "\u0001", chan); };

IRC.quit = function(msg){ IRC.send("QUIT" + (msg ? (" " + msg) : "")); };

IRC.join = function(channel, key){ IRC.send("JOIN " + channel + " " + (key || "")); };


IRC.addCmd = function(cmd, lvl, enabled, fn){ IRC.cmds[cmd] = [fn, lvl, enabled]; };


IRC.trigger = function(name, d, filter){
	if (typeof d == "function") {
		if (filter) {
			IRC.
		}
		IRC.trigger[name] ? IRC.trigger[name].push(d) : IRC.trigger[name] = [d];
	} else {
		IRC.trigger[name].each(function(v){ v(d); });
	}
};


IRC.handle = function(msg){
	if (/^PING/.test(msg)) { return IRC.send(msg.replace("PING", "PONG")); }
	
	var parsedmsg = msg.match(/^:(.*?)!.*?@(.*?)\s(.*?)\s(?:(.*?)\s)?:(.*)/);
	if (parsedmsg && IRC.handle[parsedmsg[3].toLowerCase()]) {
		IRC.handle[parsedmsg[3].toLowerCase()]({
			nick: parsedmsg[1],
			host: parsedmsg[2],
			chan: (parsedmsg[4] || "").toLowerCase(),
			msg: parsedmsg[5]
		});
	}
};

IRC.handle.nick = function(e){
	if (IRC.Uno) {
		IRC.Uno.games.each(function(v){ if (v.players[e.host]) { v.players[e.host].nick = e.msg; } });
	}
};

IRC.handle.privmsg = function(e){
	var action = e.msg.match(/^\u0001ACTION\s(.*)\u0001/i);
	var command = e.msg.match(/(?:^!|legobot:\s)([^\s]*)\s?(.*)/i);

	if (command) {
		var param = command[2];
		command = command[1].toLowerCase();
		if (IRC.cmds[command]) {
			var cmd = IRC.cmds[command];
			if (cmd[1] === 1 && !(e.host == "coldfront-B205E0FA.sip.cha.bellsouth.net" || e.host == "Wikipedia/MindstormsKid")) { return; }
			if (cmd[2] === true) { cmd[0](e, param, command); }
		}
	}
};


/********* commands *********/
IRC.addCmd("huggle", 0, true, function(e, param){ IRC.action(param ? "huggles " + param : "huggles " + e.nick, e.chan); });

IRC.addCmd("help", 0, true, function(e){
	IRC.notice('LegoBot is a bot made by MindstormsKid/LegoKid in Javascript.', e.nick);
	IRC.notice('Commands: about, huggle, say, action (commands can be said as "LegoBot: <command>" or "!<command>").', e.nick);
	IRC.notice('The above are the only commands, but you can talk to LegoBot and (maybe) it will talk back.', e.nick);
});

IRC.addCmd("say", 0, true, function(e, param){ IRC.say(param, e.chan); });

IRC.addCmd("action", 0, true, function(e, param){ IRC.action(param, e.chan); });

IRC.addCmd("join", 1, true, function(e, param){ IRC.join(param); });

IRC.addCmd("raw", 1, true, function(e, param, cmd){ IRC.send(param); });


/********* replacements *********/
LegoBot.var({
	cs: "PRIVMSG ChanServ :",
	ns: "PRIVMSG NickServ :",
	nick: function() IRC.nick
});